Shuffle Method
Shuffles (rearranges randomly) the elements of an array.
Syntax
array.Shuffle
Notes
Shuffle works on arrays of any data type and any number of dimensions. It is based on a random number. An element of the original array has a roughly equal chance of appearing in any cell of the array after the shuffle, regardless of the size and number of dimensions of the array.
Example
The following example shuffles a one-dimensional array and shows the result in a ListBox.
Dim aTest() as Integer
Dim i as Integer
aTest= Array(0,1,2,3,4,5,6,7,8,9)
aTest.Shuffle
For i = 0 to Ubound(aTest)
ListBox1.Addrow Str(aTest(i))
Next
Dim i as Integer
aTest= Array(0,1,2,3,4,5,6,7,8,9)
aTest.Shuffle
For i = 0 to Ubound(aTest)
ListBox1.Addrow Str(aTest(i))
Next
The following example shuffles a two-dimensional array.
Dim myArray(5,5) as String
Dim i, j as Integer
For i=0 to 5
For j=0 to 5
myArray(i,j)= Str(i)+ Str(j)
Next
Next
myArray.Shuffle
Dim i, j as Integer
For i=0 to 5
For j=0 to 5
myArray(i,j)= Str(i)+ Str(j)
Next
Next
myArray.Shuffle
See Also
Dim statement; Array, Join, Split, Ubound functions; Append, IndexOf, Insert, Redim, Remove, Pop, Sort methods; ParamArray keyword.